package com.copractice.data.dao.hibernate.user;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.copractice.data.dao.hibernate.HibernateDAO;
import com.copractice.data.dao.user.UserDAO;
import com.copractice.data.entity.user.User;

/**
 * Implementation of <code>HibernateDAO</code> for <code>User</code> class using <code>Hibernate</code> framework.
 * 
 * @author Dmitry Churbanov (dmitry.churbanov@gmail.com)
 * @version 1.0 03/14/2009
 */
public class HibernateUserDAO extends HibernateDAO<User, Long> implements UserDAO {
    
    private static final Logger LOG = LoggerFactory.getLogger(HibernateUserDAO.class);

    /**
     * @see com.copractice.data.dao.hibernate.HibernateDAO#getPersistenceClass
     */
    @Override
    protected Class<User> getPersistenceClass() {
        return User.class;
    }
    
    @SuppressWarnings("unchecked")
    public User findByUserName(String userName) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Find user: username = {}", userName);
        }
        
        List<User> users = getSession().getNamedQuery("USERS.getUserByName").setString("name", userName).list();
        
        if (users.size() == 0) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("User: username = {} was NOT found", userName);
            }
            return null;
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("User: username = {} was found", userName);
            }
            return users.get(0);
        }
    }

}
